'use strict';
const puppeteer = require('puppeteer');
const fs = require('fs');

const request = require('request');
const http = require('http');

(async () => {
  const startUrl = 'http://www.17bigdata.com/book/electron/index.html';
  const browser = await puppeteer.launch({
    headless: true, // 是否界面
    defaultViewport: { // 界面设置
      width: 1820,
      height: 1080,
    },
    ignoreHTTPSErrors: false, // 忽略 https 报错
    args: ['--start-maximized', '--no-sandbox', '--disable-setuid-sandbox'],
    ignoreDefaultArgs: ['--enable-automation'],
  });
  const page = await browser.newPage();
  await page.goto(startUrl).catch(error => console.log(error));

  const titles = await page.$$eval('.easyui-tree li ul li a', items => {
    return { href: items.map(item => item.href), title: items.map(item => item.innerHTML) };
  });

  // console.log(titles);
  let arr = []
  for (let i = 0; i < titles.href.length; i++) {
    arr.push({ href: titles.href[i], title: titles.title[i] })
  }
  // console.log(arr);
  // await page.goto(arr[0].href).catch(error => console.log(error))
  // await page.pdf({ path: arr[0].title + '.pdf', format: 'A2' })
  arr.map(item => {
    item.title = item.title.replace('/', '-')
    item.title = item.title.replace('.', '-')
  })
  // await traversalToPDF(arr,page)
  // await wirtePdf(arr, page)
  await startRequest(arr, page)

  await browser.close();
})();

const wirtePdf = async (links, page) => {
  let i = 0;
  while (i < links.length) {
    await page.goto(links[i].href);
    await page.waitForSelector('.easyui-tree');
    // await page.$eval('.easyui-tree', el => {
    //   el.style.display = 'none';
    // })
    await page.waitForSelector('#tt');
    const dimensions = await page.evaluate(() => {
      return {
        width: document.getElementsByClassName("easyui-layout")[0].scrollWidth + 100,
        height: document.getElementsByClassName("easyui-layout")[0].scrollHeight + 100,
        deviceScaleFactor: window.devicePixelRatio
      };
    });
    let pdf = await page.pdf({
      path: `PDF/${i + 1}.${links[i].title}.pdf`,
      format: 'A2',
      margin: {
        top: '0.5cm',
        bottom: '0.5cm',
        left: '0.5cm',
        right: '0.5cm'
      }, printBackground: true, width: dimensions.width, height: dimensions.height
    });
    console.log(`打印第${i}页pdf`);
    i++;
  }
}

const traversalToPDF = async (links, page) => {
  let i = 0;
  while (i < links.length) {
    await page.goto(links[i].href);
    await page.waitForSelector('.easyui-tree');
    // await page.$eval('.easyui-tree', el => {
    //   el.style.display = 'none';
    // })
    await page.waitForSelector('#tt');
    await page.pdf({
      path: `PDF/${i + 1}.${links[i].title}.pdf`,
      format: 'A2'
    })
    console.log(`打印第${i}页pdf`);
    i++;
  }
}


const startRequest = async (links, page) => {
  let i = 0;
  while (i < links.length) {
    await doit(links, i)
    i++;
  }
}

const doit = async (links, i) => {
  //采用http模块向服务器发起一次get请求
  http.get(links[i].href, async (res) => {
    let html = ''; //用来存储请求网页的整个html内容
    res.setEncoding('utf-8'); //防止中文乱码
    //监听data事件，每次取一块数据
    res.on('data', async (chunk) => {
      html += chunk;
    });
    //监听end事件，如果整个网页内容的html都获取完毕，就执行回调函数
    res.on('end', async () => {
      console.log(links[i].title);
      await savedContent(html, links[i].title, i);
    });
  })
}

//在本地存储所爬取的内容
const savedContent = async (html, name, i) => {
  fs.appendFile(`PDF/${i + 1}.${name}.html`, html, 'utf-8', function (err) {
    if (err) {
      console.log(err);
    } else {
      console.log(name + "成功。");
    }
  });
}